package com.zunipe.trainticketsystem;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.zunipe.trainticketsystem.database.handler.TrainHandler;
import com.zunipe.trainticketsystem.databinding.ActivityMainBinding;
import com.zunipe.trainticketsystem.ui.fragment.OrderListFragment;
import com.zunipe.trainticketsystem.ui.fragment.TrainListFragment;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;
    private FragmentManager fragmentManager;
    private TrainListFragment trainListFragment;
    private OrderListFragment orderListFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);
        fragmentManager = getSupportFragmentManager();
        trainListFragment = new TrainListFragment();
        orderListFragment = new OrderListFragment();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.train_list_layout, trainListFragment);
        transaction.add(R.id.train_list_layout, orderListFragment);
        hideAllFragment();
        transaction.show(trainListFragment);
        transaction.commit();
        // 请求通知权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1);
            }
        }

        // Debug菜单
        binding.toolbar.setOnMenuItemClickListener(item -> {
            int id = item.getItemId();
            if (id == R.id.clear_all_train) {
                TrainHandler trainHandler = new TrainHandler();
                trainHandler.clearAll();
            } else if (id == R.id.generate_new_train) {
                TrainTicketApplication.getInstance().generateNewTrain();
            }
            Intent updateHomePageListBroadcast = new Intent();
            updateHomePageListBroadcast.setAction("com.zunipe.trainticketsystem.update.home.list");
            updateHomePageListBroadcast.setPackage("com.zunipe.trainticketsystem");
            sendBroadcast(updateHomePageListBroadcast);
            return true;
        });

        // 底部导航栏点击事件
        binding.navBottom.setOnItemSelectedListener(item -> {
            int id = item.getItemId();
            hideAllFragment();
            if (id == R.id.nav_train_list) {
                FragmentTransaction transaction1 = fragmentManager.beginTransaction();
                transaction1.show(trainListFragment);
                transaction1.commit();
            } else if (id == R.id.nav_order_list) {
                FragmentTransaction transaction1 = fragmentManager.beginTransaction();
                transaction1.show(orderListFragment);
                transaction1.commit();
            }
            return true;
        });
    }


    public void hideAllFragment() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(trainListFragment);
        transaction.hide(orderListFragment);
        transaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.debug_menu, menu);
        return true;
    }
}